-
Notifications
You must be signed in to change notification settings - Fork 123
Refactor Channel creation #377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Channel creation #377
Conversation
b97d82c
to
0a6bef5
Compare
Sources/AsyncHTTPClient/ConnectionPool/HTTP1ProxyConnectHandler.swift
Outdated
Show resolved
Hide resolved
static var globalGenerator = Generator() | ||
|
||
struct Generator { | ||
private let atomic: NIOAtomic<Int> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we can make it UInt64
, just in case someone will be running it until our sun dies 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some minor suggestions, but otherwise looks good 👍
Sources/AsyncHTTPClient/ConnectionPool/HTTP1ProxyConnectHandler.swift
Outdated
Show resolved
Hide resolved
Sources/AsyncHTTPClient/ConnectionPool/HTTP1ProxyConnectHandler.swift
Outdated
Show resolved
Hide resolved
Sources/AsyncHTTPClient/ConnectionPool/HTTP1ProxyConnectHandler.swift
Outdated
Show resolved
Hide resolved
Sources/AsyncHTTPClient/ConnectionPool/HTTP1ProxyConnectHandler.swift
Outdated
Show resolved
Hide resolved
Sources/AsyncHTTPClient/ConnectionPool/HTTPConnectionPool+Factory.swift
Outdated
Show resolved
Hide resolved
var isIPAddress: Bool { | ||
var ipv4Addr = in_addr() | ||
var ipv6Addr = in6_addr() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1b08850
to
ed91313
Compare
Sources/AsyncHTTPClient/ConnectionPool/HTTP1ProxyConnectHandler.swift
Outdated
Show resolved
Hide resolved
Sources/AsyncHTTPClient/ConnectionPool/HTTP1ProxyConnectHandler.swift
Outdated
Show resolved
Hide resolved
Sources/AsyncHTTPClient/ConnectionPool/HTTP1ProxyConnectHandler.swift
Outdated
Show resolved
Hide resolved
Sources/AsyncHTTPClient/ConnectionPool/HTTP1ProxyConnectHandler.swift
Outdated
Show resolved
Hide resolved
Sources/AsyncHTTPClient/ConnectionPool/HTTP1ProxyConnectHandler.swift
Outdated
Show resolved
Hide resolved
Sources/AsyncHTTPClient/ConnectionPool/HTTPConnectionPool+Factory.swift
Outdated
Show resolved
Hide resolved
Sources/AsyncHTTPClient/ConnectionPool/HTTPConnectionPool+Factory.swift
Outdated
Show resolved
Hide resolved
Sources/AsyncHTTPClient/ConnectionPool/ChannelHandler/SOCKSEventsHandler.swift
Outdated
Show resolved
Hide resolved
Sources/AsyncHTTPClient/ConnectionPool/ChannelHandler/SOCKSEventsHandler.swift
Show resolved
Hide resolved
Sources/AsyncHTTPClient/ConnectionPool/HTTPConnectionPool+Factory.swift
Outdated
Show resolved
Hide resolved
Sources/AsyncHTTPClient/ConnectionPool/HTTPConnectionPool+Factory.swift
Outdated
Show resolved
Hide resolved
Sources/AsyncHTTPClient/ConnectionPool/HTTPConnectionPool+Factory.swift
Outdated
Show resolved
Hide resolved
#if canImport(Network) | ||
if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *), let tsBootstrap = NIOTSConnectionBootstrap(validatingGroup: eventLoop) { | ||
// create NIOClientTCPBootstrap with NIOTS TLS provider | ||
let bootstrapFuture = tlsConfig.getNWProtocolTLSOptions(on: eventLoop).map { | ||
options -> NIOClientTCPBootstrapProtocol in | ||
|
||
tsBootstrap | ||
.addTimeoutIfNeeded(self.clientConfiguration.timeout) | ||
.tlsOptions(options) | ||
.channelInitializer { channel in | ||
do { | ||
try channel.pipeline.syncOperations.addHandler(HTTPClient.NWErrorHandler()) | ||
// we don't need to set a TLS deadline for NIOTS connections, since the | ||
// TLS handshake is part of the TS connection bootstrap. If the TLS | ||
// handshake times out the complete connection creation will be failed. | ||
try channel.pipeline.syncOperations.addHandler(TLSEventsHandler(deadline: nil)) | ||
return channel.eventLoop.makeSucceededFuture(()) | ||
} catch { | ||
return channel.eventLoop.makeFailedFuture(error) | ||
} | ||
} as NIOClientTCPBootstrapProtocol | ||
} | ||
return bootstrapFuture | ||
} | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section is kind of rough to read, could it be refactored a little to have less indentation?
guard let connectTimeamount = config?.connect else { | ||
return self | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f4ce479
to
638bce9
Compare
10f24b9
to
1353129
Compare
1353129
to
f92293f
Compare
Co-authored-by: Cory Benfield <[email protected]>
b489196
to
7aec48b
Compare
|
||
func handlerAdded(context: ChannelHandlerContext) { | ||
self.proxyEstablishedPromise = context.eventLoop.makePromise(of: Void.self) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!
case .connectSent(let timeout), .headReceived(let timeout): | ||
timeout.cancel() | ||
self.failWithError(HTTPClientError.remoteConnectionClosed, context: context, closeConnection: false) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
case .connectSent, .headReceived: | ||
self.failWithError(HTTPClientError.httpProxyHandshakeTimeout, context: context) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switch self.state { | ||
case .initialized: | ||
preconditionFailure("How can we have a scheduled timeout, if the connection is not even up?") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.state = .headReceived(scheduled) | ||
case 407: | ||
self.failWithError(HTTPClientError.proxyAuthenticationRequired, context: context) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
timeout.cancel() | ||
self.state = .completed | ||
self.proxyEstablishedPromise?.succeed(()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
func handlerAdded(context: ChannelHandlerContext) { | ||
self.socksEstablishedPromise = context.eventLoop.makePromise(of: Void.self) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case .https, .https_unix: | ||
return self.makeTLSChannel(deadline: deadline, eventLoop: eventLoop, logger: logger).flatMapThrowing { | ||
channel, negotiated in | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
40d73e2
to
761b699
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
This is the first cherry pick from #376.
Utils.swift
into aConnectionFactory
ChannelHandlers
that we use for connection creation:TLSEventsHandler
got its own file and unit testsHTTP1ProxyConnectHandler
got its own file and unit testsHTTPConnectionPool
is added as a namespace to not cause major renames in follow up PRsHTTPConnectionPool.Connection.ID
and its generator were added now. (This will be used later to identify a connection during its lifetime)HTTPConnectionPool+Manager
was added to giveHTTPConnectionPool.Connection.ID.Generator
already its final destination.Looking forward to your feedback.
I'm well aware that this PR is in a race with #375. I'm happy to update it once #375 has landed.